/**
 * Created by CZ on 2017/4/6.
 */
module.exports = function ($scope,httpfactory,$state,$localStorage,$http) {
    var user = $localStorage.get('cloud-developer');
    var token = user?user.accessToken:'1';


    // ,$localStorage,OAuth,OAuthToken
    // var user1 = {
    //     "username":"admin",
    //     "password":"admin"
    // };
    // var options =  OAuth.configure({
    //     baseUrl: 'http://192.168.0.142:8080',
    //     clientId: 'client',
    //     clientSecret: 'secret' ,// optional
    //     grantPath: '/oauth/token',
    //     revokePath: '/oauth/revoke'
    // });
    // var token1 = OAuth.getAccessToken(user1, options);
    // console.log(token1);
    // // console.log(OAuth.isAuthenticated());
    // // var a =  OAuthToken.setToken(token1);
    // console.log(OAuthToken.getAccessToken());

    // httpfactory.getAccessToken().then(function(res){
    //     console.log(res);
    // },function(error){
    //     console.log(error);
    // });

    // $scope.data = {
    //     grant_type:"password",
    //     username: "admin",
    //     password: "admin",
    //     client_id: "client"
    // };
    // $scope.encoded = btoa("client_secret:secret");
    //
    // $scope.login = function() {
    //     var req = {
    //         method: 'POST',
    //         url: "http://192.168.0.142:8080/oauth/token",
    //         headers: {
    //             "Authorization": "Basic " + $scope.encoded,
    //             "Content-type": "application/x-www-form-urlencoded; charset=utf-8"
    //         },
    //         data: $httpParamSerializer($scope.data)
    //     };
    //     $http(req).then(function(data){
    //         console.log(data);
    //         $http.defaults.headers.common.Authorization =
    //             'Bearer ' + data.data.access_token;
    //         $cookies.put("access_token", data.data.access_token);
    //         window.location.href="index";
    //     });
    // }
    // var user = $localStorage.get('cloud-developer');

    $http({
        method: 'POST',
        url: "http://192.168.0.100:7088/oauth/token",
        dataType : 'Json',
        data:{
            grant_type:"password",
            client_id:"client",
            client_secret:"secret"
        },
        headers: {
            "Accept": "text/plain, */*; q=0.01",
            "Content-type": "application/x-www-form-urlencoded; charset=utf-8"
        }
        // headers: {
        //     "Content-type": "application/x-www-form-urlencoded; charset=utf-8"
        // }
    }).then(function successCallback(response) {
        //业务处理
        console.log(response);
    }, function errorCallback(error) {
        //业务处理
        console.log(error);
    });


     getWikiList(token);
    //选中数组
    $scope.checkedArry = [];
    //全选功能
    $scope.checkAll = function () {
        if ($scope.check_all) {
            $scope.checkedArry = [];
            angular.forEach($scope.WikiList, function (i) {
                i.checked = true;
                $scope.checkedArry.push(i.id);
            })
        } else {
            angular.forEach($scope.WikiList, function (i) {
                i.checked = false;
                $scope.checkedArry = [];
            })
        }
    };
    //单选功能
    $scope.checkOne = function () {
        angular.forEach($scope.WikiList, function (i) {
            var index = $scope.checkedArry.indexOf(i.id);
            if (i.checked && index === -1) {
                $scope.checkedArry.push(i.id);
            } else if (!i.checked && index !== -1) {
                $scope.checkedArry.splice(index, 1);
            }
        });

        if($scope.WikiList.length === $scope.checkedArry.length) {
            $scope.check_all = true;
        } else {
            $scope.check_all = false;
        }
    };
    
    //增加功能
    $scope.add = function(){
        //跳转到编辑页面
         $state.go('DeveloperMain.Edit')

    };
    //单个编辑
    $scope.edit = function(id){
        // console.log(id);
        //跳转到编辑页面
        $state.go('DeveloperMain.Edit',{id:id});

    };
    //删除功能
    $scope.delete = function(){
        console.log($scope.checkedArry);
        var data = '';
        //判断删除的id
        if($scope.checkedArry.length != 0){
            if($scope.checkedArry.length == 1){  //删除单个
                data = $scope.checkedArry[0].toString();
                httpfactory.deleteOneWiki(data,user.accessToken).then(function(response){
                    // console.log(response);
                    if(response.data.code == 0){
                        getWikiList();
                        $scope.checkedArry = [];
                    }
                },function(error){
                    console.log(error);
                });
            }else {  //删除多个
                data =  $scope.checkedArry.join(',');
                console.log(data);
                httpfactory.deleteWiki(data,user.accessToken).then(function(response){
                    // console.log(response);
                    if(response.data.code == 0){
                        getWikiList();
                        $scope.checkedArry = [];
                    }
                },function(error){
                    console.log(error);
                });
            }
        }

    };


    function getWikiList(token){

        httpfactory.getWikiList(token).then(function(response){
            // console.log(response);
            $scope.WikiList = response.data;
            // console.log($scope.WikiList);
        },function(error){
            console.log(error);
        });

    }
};